/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import mx.its.desarrollosw2.Categoria;
import mx.its.dsw2.biblioteca.bl.CategoriaBlLocal;

/**
 *
 * @author sistemas
 */
@ManagedBean
@RequestScoped
public class CategoryBean {
    @EJB
    private CategoriaBlLocal categoriaBl;
    private List<Categoria> lista;
    private Categoria category;
    private String accion;
    
    

    /**
     * Creates a new instance of CategoryBean
     */
    public CategoryBean() {
        category=new Categoria();
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }

    
    public List<Categoria> getLista() {
        lista=categoriaBl.getAll();
        return lista;
    }
//----------------------Acciones-----------------
   public String procesarAccion(){
       if(accion.equalsIgnoreCase("Nuevo")){
          categoriaBl.register(category); 
       }else{
           if(accion.equalsIgnoreCase("Editar")){
               categoriaBl.modify(category);
           }else{
               if(accion.equalsIgnoreCase("Delete")){
                   categoriaBl.delete(category);
               }
           }
       }
       
       return "CategoriaLista";
   }
   public void prepararNuevo(ActionEvent evt){
       accion="Nuevo";
       category=new Categoria();
       
   }
  //////-------------------
public void prepararEditar(ActionEvent evt){
    accion="Editar";
    System.out.println(evt.getComponent().getClientId());
    int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    category=lista.get(index);
}
public void prepararEliminar(ActionEvent evt){
    accion="Eliminar";
    System.out.println(evt.getComponent().getClientId());
    int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    category=lista.get(index);
}
}
